package com.cjl.multi_thread.share_cooperate.lock;

public class BusinessApp {
    static final int readWriteRatio = 10;
    static final int minThreadCount = 3;

    public static void main(String[] args) throws InterruptedException {
        GoodsInfo goodsInfo = new GoodsInfo("cc,",100,1000);
//        UseSync useSync = new UseSync(goodsInfo);
        UseLock useSync = new UseLock(goodsInfo);

        for (int i = 0; i < minThreadCount; i++) {
            Thread setT = new Thread(new SetThread(useSync));
            for (int i1 = 0; i1 < readWriteRatio; i1++) {
                Thread getT = new Thread(new GetThread(useSync));
                getT.start();
            }
            setT.start();
        }

    }
    private static class GetThread implements Runnable{
        private GoodsService service;

        public GetThread(GoodsService service) {
            this.service = service;
        }

        @Override
        public void run() {
            long l = System.currentTimeMillis();
            for (int i = 0; i < 100; i++) {
                service.getInfo();
            }
            System.out.println("GetThread.run 耗时 "+(System.currentTimeMillis()-l));
        }
    }

    private static class SetThread implements Runnable{
        private GoodsService service;

        public SetThread(GoodsService service) {
            this.service = service;
        }

        @Override
        public void run() {
            long l = System.currentTimeMillis();
            for (int i = 0; i < 10; i++) {
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                service.setNum(1);
            }
            System.out.println("---------SetThread.run 耗时 "+(System.currentTimeMillis()-l));
        }
    }
}
